//
//  LHJokeCollectionCell.m
//  Project_B
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "LHJokeCollectionCell.h"

@implementation LHJokeCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //预估高度
    _tableView.estimatedRowHeight = 260;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册两种cell
    [_tableView registerClass:[LHJokeCell class] forCellReuseIdentifier:@"LHJokeCell"];
    [_tableView registerClass:[LHJokeImageCell class] forCellReuseIdentifier:@"LHJokeImageCell"];
    [self.contentView addSubview:_tableView];
    self.dataArray = [NSMutableArray array];
    
    self.isUp = NO;
    [self addHeaderRefresh];
    [self addFooterRefresh];
}
#pragma mark ** 下拉刷新
-(void)addHeaderRefresh
{

    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUp = NO;
        [self getData];
    }];

    [_tableView.mj_header beginRefreshing];
}

#pragma mark ** 上拉加载
-(void)addFooterRefresh
{
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUp = YES;
        [self getData];
    }];
}


#pragma mark ** 获取数据
- (void)getData{
    
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/recommend/getChanRecomNews?channel=duanzi&passport=&devId=F886312A-401F-4285-A606-99866B4AB8F4&size=20"];
    [LHAFNTools appToolsWithSourcePath:url block:^(id result) {
        if (self.isUp == NO) {
            [self.dataArray removeAllObjects];
        }
        NSMutableArray *array = [result objectForKey:@"段子"];
        for (NSMutableDictionary *dic in array) {
            JokeModel *model = [[JokeModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            
            if (model.imgsrc != NULL) {
                NSArray *array = [model.pixel componentsSeparatedByString:@"*"];
                model.picH = [array[1] floatValue];
            }
            //内容高度
            CGSize size = [SizeForLabel labelRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT)  LabelText:model.digest Font:[UIFont systemFontOfSize:14.5]];
            model.h = size.height;
            //up宽度
            CGSize sizeUp = [[NSString stringWithFormat:@"%ld",(long)model.upTimes] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
            model.upW = sizeUp.width;
            //down宽度
            CGSize sizeDown = [[NSString stringWithFormat:@"%ld",(long)model.downTimes] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
            model.downW = sizeDown.width;
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JokeModel *model = [_dataArray objectAtIndex:indexPath.item];
    //有图
    if (model.imgsrc == NULL) {
        LHJokeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHJokeCell" forIndexPath:indexPath];
        cell.jokeModel = model;
        return cell;
    }else{
        //无图
        LHJokeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHJokeImageCell" forIndexPath:indexPath];
        cell.jokeModel = model;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [[_dataArray objectAtIndex:indexPath.row] h] + [[_dataArray objectAtIndex:indexPath.row] picH] + 70;
}
@end
